package com.bai.Controller;

import com.bai.Pojo.Admin;
import com.bai.Pojo.Prisoner;
import com.bai.Service.PrisonerService;
import com.bai.Utils.DtoResultUtil;
import com.bai.Utils.FastJsonUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @title: PrisonerController
 * @Author BaiSc
 * @Date: 2021/6/13 18:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/Prisoner")
public class PrisonerController {
    @Autowired
    private PrisonerService prisonerService;

    //查询所有犯人
    @GetMapping("/Pri_table")
    public PageLimitDto QueryAllAdmin(String prisex,String partition, String charge,Integer offset, Integer limit){
        PageLimitDto result =prisonerService.QueryAll(prisex,partition,charge,offset,limit);
        return result;
    }

    //添加犯人
    @RequiresRoles(value={"admin","safe"},logical= Logical.OR)
    @GetMapping("/add")
    public PublicResponseBody add(Prisoner prisoner){
        PublicResponseBody addPrisoner = prisonerService.addPrisoner(prisoner);
        if (addPrisoner!=null){
            if (addPrisoner==null){
                return DtoResultUtil.resultError();
            }
            return addPrisoner;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //删除
    @RequiresRoles(value={"admin","safe"},logical= Logical.OR)
    @GetMapping("/delete")
    public PublicResponseBody delete(Integer id){
        if (id!=null){
            PublicResponseBody result = prisonerService.deletePrisonerById(id);
            if (result==null){
                return DtoResultUtil.resultError();
            }
            return result;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }

    //查看囚犯所有细节
    @RequiresRoles(value={"admin","safe"},logical= Logical.OR)
    @GetMapping("/getDetail")
    public PublicResponseBody getDetail(Integer id){
        if (id!=null){
            PublicResponseBody queryPrisonerById = prisonerService.queryPrisonerById(id);
            if (queryPrisonerById!=null){
                if (queryPrisonerById==null){
                    return DtoResultUtil.resultError();
                }
                return queryPrisonerById;
            }else {
                return DtoResultUtil.resultParamError();
            }
        }
        return DtoResultUtil.resultParamError();
    }

    //编辑修改
    @RequiresRoles(value={"admin","safe"},logical= Logical.OR)
    @PostMapping("/edit")
    public PublicResponseBody edit(Prisoner prisoner){
        PublicResponseBody updatePrisoner = prisonerService.UpdatePrisoner(prisoner);
        if (updatePrisoner!=null){
            if (updatePrisoner==null){
                return DtoResultUtil.resultError();
            }
            return updatePrisoner;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }


    //得到囚犯简介
    @RequiresRoles(value={"admin","safe"},logical= Logical.OR)
    @GetMapping("/queryPrisonerDetail")
    public PublicResponseBody  queryPrisonerDetailsById(Integer id) {
        PublicResponseBody prisoner = prisonerService.queryPrisonerDetailsById(id);
        if (prisoner!=null){
            if (prisoner==null){
                return DtoResultUtil.resultError();
            }
            return prisoner;
        }else {
            return DtoResultUtil.resultParamError();
        }
    }
}
